package org.jeecg.controller;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.domain.User;
import org.jeecg.result.ResultTool;
import org.jeecg.service.FansService;
import org.jeecg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/fans")
public class FansController {

    @Autowired
    private FansService fansService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/getFans",method = RequestMethod.GET)
    public ResultTool getCares(@RequestParam("username") String username){
        if (username == null){
            return new ResultTool(400,"获取失败");
        }
        User user = userService.queryByUsername(username);
        if (user == null){
            return new ResultTool(400,"获取失败");
        }
        List<User> fans = fansService.getFans(username);
        return new ResultTool(fans,200);
    }


    @RequestMapping(value = "/addFans",method = RequestMethod.POST)
    public ResultTool addCare(@RequestBody JSONObject jsonObject){
        String username = (String) jsonObject.get("username");
        String fanUsername = (String) jsonObject.get("fanUsername");
        User user = userService.queryByUsername(username);
        User fanUser = userService.queryByUsername(fanUsername);
        if (user == null || fanUser == null){
            return new ResultTool(400,"关注失败");
        }
        Boolean result = fansService.addFan(username, fanUsername);
        if (result){
            return new ResultTool(200,"关注成功");
        }
        return new ResultTool(400,"关注失败");
    }

    @RequestMapping(value = "/cancelFans",method = RequestMethod.POST)
    public ResultTool cancelCare(@RequestBody JSONObject jsonObject){
        String username = (String) jsonObject.get("username");
        String fanUsername = (String) jsonObject.get("fanUsername");
        User user = userService.queryByUsername(username);
        User careUser = userService.queryByUsername(fanUsername);
        if (user == null || careUser == null){
            return new ResultTool(400,"取消关注失败");
        }

        Boolean tempResult = fansService.queryFans(username, fanUsername);
        if (tempResult){
            Boolean result = fansService.cancelCare(username, fanUsername);
            if (result){
                return new ResultTool(200,"取消关注成功");
            }
        }
        return new ResultTool(400,"取消关注失败");
    }

    @RequestMapping(value = "/queryFansByUserAndGoodId",method = RequestMethod.GET)
    public ResultTool queryCareByUserAndGoodId(@RequestBody JSONObject jsonObject){
        String username = (String) jsonObject.get("username");
        String careUsername = (String) jsonObject.get("fanUsername");
        if (username == null || careUsername == null){
            return new ResultTool(400,"获取失败");
        }
        Boolean result;
        try {
            result = fansService.queryFans(username, careUsername);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultTool(400,"查询失败");
        }
        if (result){
            return new ResultTool(true,200);
        }
        return new ResultTool(false,200);
    }

}
